/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit;

public class Injector {
	private static final InjectorAPI implementation = createImplementation();

	public static <T> T injectCollaborator() {
		return implementation.injectCollaborator();
	}

	public static <T> T injectCollaborator(Class<?> callingClass) {
		return implementation.injectCollaborator(callingClass);
	}

	public static <T> T injectFixture() {
		return implementation.injectFixture();
	}

	public static <T> T injectFixture(Class<?> callingClass) {
		return implementation.injectFixture(callingClass);
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Implementation methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	private static Class getClassProperty(String name, String defaultValue) throws ClassNotFoundException {
		return Class.forName(System.getProperty(name, defaultValue));
	}

	private static InjectorAPI createImplementation() {
		try {
			Class implementation = getClassProperty("picounit.injector",
				"picounit.features.static_mocking.InjectorImpl");

			return (InjectorAPI) implementation.getMethod("instance").invoke(null);
		}
		catch (Exception e) {
			throw new PicoUnitException(e);
		}
	}
}
